<?php
class Avipapply {
	public static function main($funcode) {
		switch ($funcode) {
			case 1:
				sview::verify(true, true);
				self::__docard();
				break;
			case 2:
				sview::verify(true, true);
				self::__doapply();
				break;
			case 3:
				sview::verify(true, true);
				self::__dostatus();
				break;
			case 4:
				sview::verify(true, true);
				self::__docancel();
				break;
			case 5:
				sview::verify();
				self::__dortmp();
				break;
			case 6:
				sview::verify(true, true);
				self::__dolist();
				break;
			case 7:
				sview::verify(true, true);
				self::__dostart();
				break;
			case 8:
				sview::verify(true, true);
				self::__dostop();
				break;
			case 9:
				sview::verify(true, true);
				self::__doreject();
				break;
			case 10:
				sview::verify(true, true);
				self::__dogetcard();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//获取当前用户剩余的连麦卡5-7-10
	private static function __dogetcard() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$nums   = api_mypros::dototal($userid, api_vipapply::PropId);
		$data   = array('nums'=>$nums);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//获取麦卡，启动进入应用之后调用5-7-1
	private static function __docard() {
		$acl  = acl::getinst('vedio');
		$nums = 0;
		$userid = $acl->user['id'];
		$rlog   = model::load('live/collaborationcard')->get_one($userid);
		if (!$rlog) {
			$fields = array('userid'=>$userid, 'ip'=>get_ip(), 'stime'=>config::$nowtime);
			model::load('live/collaborationcard')->new_one($fields);
			lang::load('sitemsg.php');
			$title  = lang::text(50028);
			$msg    = lang::text(50029);
			api_sitemsg::send($title, $msg, $userid);
			$refid  = api_vipapply::PropId;
			$nums   = 3;
			api_mypros::doprocess($userid, $nums, $refid);
			$flag   = 1;
		}
		$data = array('nums'=>$nums);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//用户连麦申请5-7-2
	private static function __doapply() {
		$acl      = acl::getinst('vedio');
		$userid   = $acl->user['id'];
		$sid      = (int)sview::$ctrl['studioid'];
		$evtid    = (int)sview::$ctrl['eventid'];
		$studio   = model::load('live/studio')->get_one($sid);
		if (!$studio || $studio['olstatus'] != 1) {
			sview::$view->assign_error(80138, lang::text(80138));
		}
		//判断是否已经申请过了
		$data     = api_vipapply::get_vip_apply($userid, $sid, $evtid);
		$vipapp   = stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? api_vipapply::IPhone:api_vipapply::Android;
		$status   = api_vipapply::dostatus($data, $userid, $sid, $evtid, $vipapp);
		if ($status != -1 && $status != 2) {//当前的禁用状态，不允许重复申请
			$msg  = ($status == 3)? lang::text(80139):lang::text(80140);
			sview::$view->assign_error(80018, $msg);
		}
		//扣除道具进入申请状态
		$nums     = api_vipapply::deduct_prop(sview::$view, $userid, $sid, 1);
		api_vipapply::dovipapply($userid, $sid, $evtid, $nums, $studio['userid'], $vipapp);
		sview::$view->display();
	}
	
	//验证用户是否连麦5-7-3
	private static function __dostatus() {
		$acl      = acl::getinst('vedio');
		$userid   = $acl->user['id'];
		$sid      = (int)sview::$ctrl['studioid'];
		$evtid    = (int)sview::$ctrl['eventid'];
		$data     = api_vipapply::get_vip_apply($userid, $sid, $evtid);
		$vipid    = empty($data)? 0:$data['id'];
		$vipapp   = stripos($_SERVER['HTTP_USER_AGENT'], 'ios')? api_vipapply::IPhone:api_vipapply::Android;
		$status   = api_vipapply::dostatus($data, $userid, $sid, $evtid, $vipapp);
		$data     = array('status'=>$status, 'id'=>$vipid);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//会员取消连麦申请5-7-4
	private static function __docancel() {
		$view   = view::getinst('apijson');
		$acl    = acl::getinst('vedio');
		$sid    = (int)sview::$ctrl['studioid'];
		$studio = model::load('live/studio')->get_one($sid);
		if ($studio) {
			$evtid  = (int)sview::$ctrl['eventid'];
			$userid = $acl->user['id'];
			api_vipapply::doreject($userid, $sid, $evtid, $studio['userid'], 0);
		}
		sview::$view->display();
	}
	
	//获取直播间连麦rtmp地址信息5-7-5
	private static function __dortmp() {
		$push_rtmp = array();
		$recv_rtmp = array();
		$vipuser   = array();
		$sid       = (int)sview::$ctrl['studioid'];
		$studio    = model::load('live/studio')->get_one($sid);
		$ispush    = 0;
		if ($studio && $studio['vipuserid']) {
			$acl    = acl::getinst('vedio');
			$userid = ($acl->user)? $acl->user['id']:0;
			$viper  = model::load('live/userinfo')
				->set_devision($studio['vipuserid'])->get_one($studio['vipuserid']);
			$lv     = api_lv::item($viper['lv']);
			$nick   = htmlspecialchars_decode($viper['nickname']);
			$vipuser= array('lvicon'=>$lv['iconsrc'], 'lv'=>$viper['lv'],
				'userid'=>$studio['vipuserid'], 'avtar'=>$viper['thumb'], 'nick'=>$nick);
			if ($userid == $studio['vipuserid']) {
				$ispush = 1;
				$push_rtmp = api_rtmp::vip_push($sid, $studio['vipuserid'], $studio['vipapp']);
				$recv_rtmp = api_rtmp::vip_near($sid, -1);//就近的主播rtmp地址
			} else if ($userid == $studio['userid']) {
				$recv_rtmp = api_rtmp::vip_near($sid, $studio['vipuserid'], $studio['vipapp']);
			} else {
				$recv_rtmp = api_rtmp::vip_recv($sid, $studio['vipuserid']);
			}
		}
		$data = array('ispush'=>$ispush, 'vipuser'=>$vipuser, 'push_rtmp'=>$push_rtmp, 'recv_rtmp'=>$recv_rtmp);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//获取直播间连麦申请接口5-7-6
	private static function __dolist() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$sid    = (int)sview::$ctrl['studioid'];
		$studio = model::load('live/studio')->get_one($sid);
		$list   = array();
		if ($userid == $studio['userid']) {
			$list = api_vipapply::dolist($sid);
		}
		$data = array('list'=>$list);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//获取直播间连麦申请列表5-7-7
	private static function __dostart() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$vipid  = (int)sview::$ctrl['id'];
		$data   = api_vipapply::doaccept(sview::$view, $vipid, $userid);
		if (!$data) {
			sview::$view->assign_error(80141, lang::text(80141));
		}
		$rtmp   = api_rtmp::vip_near($data['studioid'], $data['userid'], $data['vipapp']);
		$data   = array('rtmp'=>$rtmp, 'viper'=>$data['viper']);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//主播/连麦观众结束本次连麦5-7-8
	private static function __dostop() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$sid    = (int)sview::$ctrl['studioid'];
		api_vipapply::dofinish(sview::$view, $sid, $userid);
		sview::$view->display();
	}
	
	//主播拒绝观众连麦申请5-7-9
	private static function __doreject() {
		$acl    = acl::getinst('vedio');
		$evtid  = 0;
		$sid    = (int)sview::$ctrl['studioid'];
		$userid = (int)sview::$ctrl['userid'];
		$myuid  = $acl->user['id'];
		$studio = model::load('live/studio')->get_one($sid);
		if (!$studio || $studio['userid'] != $myuid) {
			$view->assign_error(80009, lang::text(80009));
		}
		api_vipapply::doreject($userid, $sid, $evtid, $studio['userid']);
		sview::$view->display();
	}
}